package com.power.using.mydaggerp.ui.fragment.gankio.child.tabs;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.power.using.mydaggerp.R;
import com.power.using.mydaggerp.adapter.GankIoDayAdapter;
import com.power.using.mydaggerp.base.BasePresenter;
import com.power.using.mydaggerp.base.fragment.BaseRecycleFragment;
import com.power.using.mydaggerp.contract.gankio.tabs.GankIoDayContract;
import com.power.using.mydaggerp.model.bean.gankio.GankIoDayItemBean;
import com.power.using.mydaggerp.presenter.gankio.tabs.GankIoDayPresenter;
import com.zyw.horrarndoo.sdk.rxbus.RxBus;

import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2019/10/14.
 * Time: 2019/10/14  9:53
 */
public class GankIoDayFragment extends BaseRecycleFragment<GankIoDayContract.GankIoDayPresenter>
        implements GankIoDayContract.IGankioDayView {

    @BindView(R.id.rv_gankio_day)
    RecyclerView rvGankioDay;
    Unbinder unbinder;
    private GankIoDayAdapter mGankIoDayAdapter;

    public static GankIoDayFragment newInstance() {
        Bundle args = new Bundle();
        GankIoDayFragment fragment = new GankIoDayFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initData() {
        super.initData();
        RxBus.get().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unRegister(this);

    }

    @Override
    protected void onErrorViewClick(View view) {
        mPresenter.loadLatestList();
    }

    @Override
    protected void showLoading() {
        mGankIoDayAdapter.setEmptyView(loadingView);
    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return GankIoDayPresenter.newInstance();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gank_io_day;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {

        mGankIoDayAdapter = new GankIoDayAdapter(null);
        rvGankioDay.setAdapter(mGankIoDayAdapter);
        rvGankioDay.setLayoutManager(new LinearLayoutManager(mActivity));

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.loadLatestList();
    }


    @Override
    public void updateContentList(List<GankIoDayItemBean> list) {

        if(mGankIoDayAdapter.getData().size()==0){
            initRecycleView(list);
        }else{
            mGankIoDayAdapter.addData(list);
        }
    }


    @Override
    public void itemNotifyChanged(int position) {

    }

    @Override
    public void itemNotifyChanged(int position, GankIoDayItemBean bean) {
        mGankIoDayAdapter.refeshItem(position, bean);
    }


    @Override
    public void showNetworkError() {
        mGankIoDayAdapter.setEmptyView(errorView);
    }

    @Override
    public void showLoadMoreError() {

    }

    @Override
    public void showNoMoreData() {

    }

    private void initRecycleView(List<GankIoDayItemBean> list) {

        mGankIoDayAdapter = new GankIoDayAdapter(list);
        mGankIoDayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mPresenter.onItemClick(position, (GankIoDayItemBean) adapter.getItem(position));
            }
        });

        mGankIoDayAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch(view.getId()){
                    case R.id.ll_more:
                        mPresenter.onMoreClick(position, (GankIoDayItemBean) adapter.getItem(position));
                        break;

                    case R.id.ll_refesh:
                        mPresenter.onRefeshClick(position, (GankIoDayItemBean) adapter.getItem(position));

                        break;
                }
            }
        });

        rvGankioDay.setAdapter(mGankIoDayAdapter);

    }



}
